<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='发布文章',active='publish'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<link th:href="@{/back/plugins/tagsinput/jquery.tagsinput.css}" rel="stylesheet"/>
<link th:href="@{/back/plugins/select2.dist.css/select2-bootstrap.css}" rel="stylesheet"/>
<link th:href="@{/back/plugins/toggles/toggles.css}" rel="stylesheet"/>

<link th:href="@{/back/plugins/mditor/css/mditor.min.css}" rel="stylesheet"/>
<link th:href="@{/back/plugins/dropzone/4.3.0/min/dropzone.min.css}" rel="stylesheet">
<style>
    #tags_tagsinput {
        background-color: #fafafa;
        border: 1px solid #eeeeee;
    }

    #tags_addTag input {
        width: 100%;
    }

    #tags_addTag {
        margin-top: -5px;
    }

    .mditor .editor{
        font-size: 14px;
        font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    }
    .mditor .backdrop, .mditor .textarea, .mditor .viewer{
        font-size: 14px;
    }
    .markdown-body{
        font-size: 14px;
    }
    .note-toolbar {
        text-align: center;
    }

    .note-editor.note-frame {
        border: none;
    }

    .note-editor .note-toolbar {
        background-color: #f5f5f5;
        padding-bottom: 10px;
    }

    .note-toolbar .note-btn-group {
        margin: 0;
    }

    .note-toolbar .note-btn {
        border: none;
    }

    #articleForm #dropzone {
        min-height: 200px;
        background-color: #dbdde0;
        line-height:200px;
        margin-bottom: 10px;
    }
    #articleForm .dropzone {
        border: 1px dashed #8662c6;
        border-radius: 5px;
        background: white;
    }
    #articleForm .dropzone .dz-message {
        font-weight: 400;
    }
    #articleForm .dropzone .dz-message .note {
        font-size: 0.8em;
        font-weight: 200;
        display: block;
        margin-top: 1.4rem;
    }
</style>
<body class="fixed-left">
<!--模态框-->
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="removeadd()"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">创建文章分类</h4>
            </div>
            <div class="modal-body">
                分类名称：&nbsp;<input type="text" class="form-control" id="insertOptionValue"/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default cancle-btn" data-dismiss="modal" onclick="removeadd()">取消</button>
                <button type="button" class="btn btn-primary confirm-btn" onclick="insertoption(this)">确定</button>
            </div>
        </div>
    </div>
</div>
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">
                            <th:block th:if="${null != contents}">
                                编辑文章
                            </th:block>
                            <th:block th:unless="${null != contents}">
                                发布文章
                            </th:block>
                        </h4>
                    </div>
                    <div class="col-md-12">
                        <form id="articleForm">
                            <input type="hidden" name="id"
                                   th:value="${contents!=null and contents.id!=null}?${contents.id}: ''" id="id"/>
                            <input type="hidden" name="allow_comment"
                                   th:value="${contents!=null and contents.allow_comment !=null}
                                   ?${contents.allow_comment}: 1" id="allow_comment"/>
                            <input type="hidden" name="content" id="content-editor"/>
                            <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
                            <div class="form-group col-md-6" style="padding: 0 10px 0 0;">
                                <th:block th:if="${null != contents}">
                                    <input type="text" class="form-control" name="title"
                                           required="required" aria-required="true" th:value="${contents.title}"/>
                                </th:block>
                                <th:block th:unless="${null != contents}">
                                    <input type="text" class="form-control" placeholder="请输入文章标题（必须）" name="title"
                                           required="required" aria-required="true"/>
                                </th:block>
                            </div>
                            <div class="form-group col-md-6" style="padding: 0 10px 0 0;">
                                <th:block th:if="${null != contents}">
                                    <input name="tags" id="tags" type="text" class="form-control" th:value="${contents.tags}" />
                                </th:block>
                                <th:block th:unless="${null != contents}">
                                    <input name="tags" id="tags" type="text" class="form-control" placeholder="请输入文章标签" />
                                </th:block>
                            </div>
<!--                            请选择文章分类-->
                            <div class="form-group col-md-6" style="padding: 0 10px 0 0;">
                                <select class="form-control" name="categories" id="SelectCateId">



                                    <th:block th:each="cat : ${categoriesList}">
                                        <th:block th:if="${null != categories && categories==cat}">
                                            <option style="text-align: center;" selected="selected"><th:block th:text="${cat}"/></option>
                                        </th:block>
                                        <th:block th:if="${null == categories || categories!=cat}">
                                            <option style="text-align: center;"><th:block th:text="${cat}"/></option>
                                        </th:block>



                                    </th:block>
                                    <option style="background-color: #d5d5d5;text-align: center;color: green" value="insertId" >+新增类别</option>
                                </select>
                            </div>
<!--                            文章内容-->
                            <div class="clearfix"></div>
                            <div id="md-container" class="form-group">
                                <textarea id="md-editor" th:utext="${contents!=null and contents.content !=null}?${contents.content}: ''"></textarea>
                            </div>

                             <div class="form-group col-md-3 col-sm-4">
                                   <label class="col-sm-4">开启评论</label>
                                   <div class="col-sm-8">
                                       <div th:class="${contents!=null and contents.allow_comment!=null }?'toggle toggle-success allow-'+${contents.allow_comment}:'toggle toggle-success allow-true'"></div>
                                   </div>
                               </div>

                            <div class="clearfix"></div>
                            <div class="form-group col-md-3 col-sm-4">
                                <label>
                                    <th:block th:if="${null != contents}">
                                        <th:block th:if="${contents.secret}">
                                            <input type="checkbox" name="secret" value="true" checked> 私密文章
                                        </th:block>
                                        <th:block th:unless="${contents.secret}">
                                            <input type="checkbox" name="secret" value="true"> 私密文章
                                        </th:block>
                                    </th:block>
                                    <th:block th:unless="${null != contents}">
                                        <input type="checkbox" name="secret" value="true"> 私密文章
                                    </th:block>

                                </label>
                            </div>

                            <div class="text-right">
                                <a class="btn btn-default waves-effect waves-light" th:href="@{/admin/article}">返回列表</a>
                                <button type="button" class="btn btn-primary waves-effect waves-light" onclick="subArticle('publish');">
                                    保存文章
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<script th:inline="javascript">

    //添加分类项
    var selectId = document.getElementById("SelectCateId");//获取ID
    selectId.onchange = function()//触发事件
    {
        var adssdd = selectId.options[selectId.selectedIndex].hasAttribute('value');//获取选中文本

        if(adssdd){
            this.parentNode.firstElementChild.firstElementChild.selected=true;
            document.getElementById("myModal").setAttribute("class","modal fade in");
            document.getElementById("myModal").setAttribute("style","display: block;");

        }
    }
    function removeadd(){
        document.getElementById("myModal").setAttribute("class","modal fade");
        document.getElementById("myModal").removeAttribute("style");
    }
    function insertoption(ev){

        var tex = document.getElementById("insertOptionValue").value;
        document.getElementById("insertOptionValue").value="";
        if(tex==null||tex=='') {
            alert("不能为空");
            return ;
        }
        var ele=document.createElement('option');
        ele.setAttribute("style","text-align: center;");
        ele.setAttribute("selected",true);
        ele.innerHTML=tex;

        var len = selectId.childNodes.length;
        selectId.insertBefore(ele,selectId.childNodes[len-2]);

        removeadd();
    }
    //上传图片
    function mdImageAjax(index){
        //var submit = $("#ImageSubmit").submit();
        //alert(submit);

        //$("#uploadframe").val;

        //return ;
        var formData = new FormData($('#ImageSubmit')[0]);
        //alert(formData.get("name"));
        if(formData.toString().trim().length<=0)return ;
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        $.ajax({
            type: 'POST',
            url: '/admin/article/uploadImage',
            //url: /*[[@{/admin/article/uploadImage}]]*/,
            data: formData,
            async: true,
            contentType: false,
            beforeSend : function(xhr) {
                xhr.setRequestHeader(header, token);
            },
            /**
             * 必须false才会避开jQuery对 formdata 的默认处理
             * XMLHttpRequest会对 formdata 进行正确的处理
             */
            processData: false,
            //dataType: 'json',
            success: function (result) {
                //alert(result);location.href.replace(location.pathname,"")+
                //alert(result);
                add("textarea",result);

            },
            error: function () {
                alert("上传失败，可能超过图片大小上传限制")
            }
        });

    }
    $(function (){
        setTimeout(function(){

            $("i[data-cmd='image']").click(function () {
                document.getElementById("mdImage").click();
            });
        },1000);

    });
    // 光标处插入内容
    function add(id,str) {
        var tc = document.getElementsByClassName(id)[0];
        var tclen = tc.value.length;
        tc.focus();
        // 兼容性检查
        if (typeof document.selection != "undefined") {
            document.selection.createRange().text = str;
        } else {
            tc.value =
                // 获取光标的开始位置
                tc.value.substr(0, tc.selectionStart) +
                str +
                tc.value.substring(tc.selectionStart, tclen);
        }
    }




</script>
<form th:action="@{/admin/article/uploadImage}" method="post" enctype="multipart/form-data" id="ImageSubmit">
    <input type="file" name="mdImage" id="mdImage" accept="image/*" onchange="mdImageAjax();" style="display: none">
</form>
<div th:replace="back/footer :: footer"></div>

<script th:src="@{/back/plugins/tagsinput/jquery.tagsinput.min.js}"></script>
<script th:src="@{/back/plugins/jquery-multi-select/jquery.quicksearch.js}"></script>

<script th:src="@{/back/plugins/mditor/js/mditor.min.js}"></script>
<script th:src="@{/back/plugins/multi-select/0.9.12/js/jquery.multi-select.min.js}"></script>
<script th:src="@{/back/plugins/select2/3.4.8/select2.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-toggles/2.0.4/toggles.min.js}"></script>
<script th:src="@{/back/plugins/dropzone/4.3.0/min/dropzone.min.js}"></script>
<script th:src="@{/back/plugins/to-markdown/3.1.0/to-markdown.min.js}"></script>
<!--<script th:src="@{/assets/js/jquery.min.js}"></script>-->
<script th:src="@{/back/js/article.js}"></script>
</body>
<script>

</script>
</html>